/**
 * 
 */
package gamePlay;

import java.util.List;

import datatype.Question;
import datatype.Statistics;
import interfaces.Chain;

/**
 * @author Yuval implements the chain interface, the game questions are held
 *         here, with all the function that control the game flow
 */
public abstract class AbsChain implements Chain {

	protected List<Question> questions;
	private long startTime = 0;
	private int currentQuestion = 0;
	private int score = 0;

	// help/life

	public List<Question> getQuestions() {
		return questions;
	}

	public AbsChain(List<Question> questions) {
		super();
		this.questions = questions;
	}

	public void setQuestions(List<Question> questions) {
		this.questions = questions;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public int getCurrentQuestion() {
		return currentQuestion;
	}

	public void setCurrentQuestion(int currentQuestion) {
		this.currentQuestion = currentQuestion;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	@Override
	public Question startChain(long time) {
		this.startTime = time;
		return questions.get(currentQuestion);
	}

	@Override
	public boolean chooseAnswer(int answer) {
		Question q = questions.get(currentQuestion);

		if (questions.get(currentQuestion).isCorrectAnswer(answer)) {
			score += q.getPoints();
			currentQuestion++;
			return true;
		}

		return false;
	}

	@Override
	public Question nextQuestion() {
		if (questions.size() > currentQuestion)
			return questions.get(currentQuestion);
		return null;
	}

	@Override
	public void finish(long time, Statistics stats) {
		stats.setScore(stats.getScore() + score);
		stats.setTotalCorrectAnswers(stats.getTotalCorrectAnswers()
				+ currentQuestion);
		stats.setTotlaTime(stats.getTotlaTime() + (time - startTime));
		stats.setFinish(questions.size() == currentQuestion);

	}

	@Override
	public double getRatio() {
		return (double) currentQuestion / questions.size();
	}

}
